﻿using CPPEI.ETL.DataServiceInterface;
using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using SER = CPPEI.ETL.Domain.Services;
using VIW = CPPEI.ETL.Domain.Views;

namespace CPPEI.ETL.WebApi.AppExtract
{
    /// <summary>
    /// 获取数据的批次记录，根据数据的唯一标识信息来获取，同一条数据反复抽取就会生成多个批次记录
    /// </summary>
    [CPPEI.WebApi.AuthGetAction("AppExtract", "GetDataBatchList", "1.0")]
    public class GetDataBatchListAction : BaseAction<GetDataBatchListParams, GetDataBatchListResult>
    {
        protected override ActionResult<GetDataBatchListResult> OnExecute(ActionContext context,
            SysParameters sysParameters,
            GetDataBatchListParams opParams)
        {
            SER.AppExtractService service = new SER.AppExtractService();

            ServiceResult<VIW.VDataBatchListDetail> serResult = service.GetDataBatchListDetail(opParams.ServiceType, opParams.Source, 
                opParams.BatchId,
                opParams.DataIdentity,
                opParams.Page * 25, 25);
            if (serResult.IsSuccess)
            {
                GetDataBatchListResult actResult = new GetDataBatchListResult();
                actResult.Page = opParams.Page;
                actResult.Start = serResult.Data.DtTableData.Start;
                actResult.Limit = serResult.Data.DtTableData.Limit;
                actResult.Total = serResult.Data.DtTableData.Total;
                actResult.Count = serResult.Data.DtTableData.DataTable.Rows.Count;


                actResult.Source = serResult.Data.Source;

                actResult.SourceText = serResult.Data.SourceText;

                actResult.ServiceType = serResult.Data.Source;

                actResult.ServiceText = serResult.Data.ServiceText;

                actResult.DtServiceId = serResult.Data.DtServiceId;
                actResult.DtServiceText = serResult.Data.DtServiceText;

                actResult.DtSourceId = serResult.Data.DtSourceId;

                actResult.DtSourceText = serResult.Data.DtSourceText;

                List<string> columns = new List<string>();
                List<string> types = new List<string>();
                for (int colIndex = 0; colIndex < serResult.Data.DtTableData.DataTable.Columns.Count; colIndex++)
                {
                    columns.Add(serResult.Data.DtTableData.DataTable.Columns[colIndex].ColumnName);
                    types.Add(serResult.Data.DtTableData.DataTable.Columns[colIndex].DataType.FullName);
                }
                actResult.Columns = columns;
                actResult.Types = types;

                for (int rowIndex = 0; rowIndex < serResult.Data.DtTableData.DataTable.Rows.Count; rowIndex++)
                {
                    System.Data.DataRow dataRow = serResult.Data.DtTableData.DataTable.Rows[rowIndex];
                    GetDataBatchListResult.Item item = new GetDataBatchListResult.Item();

                    for (int colIndex = 0; colIndex < actResult.Columns.Count; colIndex++)
                    {
                        item.Add(Convert.ToString(dataRow[actResult.Columns[colIndex]]));
                    }

                    actResult.Items.Add(item);
                }
                return new ActionResult<GetDataBatchListResult>(actResult);
            }
            else
            {

                return new ActionResult<GetDataBatchListResult>(serResult.IsSuccess, serResult.ErrorCode, serResult.Message, null);
            }
        }
    }

    public class GetDataBatchListParams
    {
        public Guid ServiceType { get; set; }
        /// <summary>
        /// 数据源
        /// </summary>
        public Guid Source { get; set; }

        public string DataIdentity { get; set; }

        public Guid BatchId { get; set; }

        public int Page { get; set; }
    }

    public class GetDataBatchListResult : PagedResult
    {

        /// <summary>
        /// 外部程序抽取数据时对应的数据源信息（这个就是DtTableId）
        /// </summary>
        public Guid Source { get; set; }

        /// <summary>
        /// 外部程序抽取数据时对应的数据源名称（这个就是DtTable的名称）
        /// </summary>
        public string SourceText { get; set; }

        public Guid ServiceType { get; set; }

        public string ServiceText { get; set; }

        /// <summary>
        /// 本系统内部向数据表抽数的数据服务Id
        /// </summary>
        public Guid DtServiceId { get; set; }
        /// <summary>
        /// 本系统内部向数据表抽数的数据服务名称
        /// </summary>
        public string DtServiceText { get; set; }

        /// <summary>
        /// 本系统内部向数据表抽数的数据源id
        /// </summary>
        public Guid DtSourceId { get; set; }

        /// <summary>
        /// 本系统内部向数据表抽数的数据源名称
        /// </summary>
        public string DtSourceText { get; set; }

        public List<string> Columns { get; set; }
        public List<string> Types { get; set; }
        public List<Item> Items = new List<Item>();

        public class Item
        {
            public List<string> Values { get; set; }

            public Item()
            {
                Values = new List<string>();
            }

            public void Add(string value)
            {
                Values.Add(value);
            }
        }
    }
}